<?php

/**
 * Usuario form.
 *
 * @package    cronos-doctrine
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class UsuarioForm extends BaseUsuarioForm
{
  public function configure()
  {
      

      $this->removeField();
      
      $this->widgetSchema['empresa_id'] = new sfWidgetFormInputHidden(array(),array('value'=> sfContext::getInstance()->getUser()->getAttribute('agente_user_empresa_id')));
      
      
      
      //Cambiar a lista y su validador de los status Activo e Inativo 0/1
      $this->widgetSchema['status'] = new sfWidgetFormChoice(array(
         'choices' => Usuario::$status,
         'expanded' => false,
         'multiple' => false,

      ));

      $this->validatorSchema['status'] = new sfValidatorChoice(array(
          'choices' => array_keys(Usuario::$status),

      ));

      
      $this->widgetSchema['pais_id'] = new sfWidgetFormInputHidden(array(),array('value'=> 1));
      
       $this->widgetSchema['entidad_id'] = new sfWidgetFormInputHidden(array(),array('value'=> sfContext::getInstance()->getUser()->getAttribute('agente_user_entidad_id')));
       
       $this->widgetSchema['municipio_id'] = new sfWidgetFormDoctrineChoice(array(
         'multiple' => false, 
         'add_empty'   => 'Seleccione delegacion / municipio',
         'model' => 'Municipio',
         'query' => Doctrine_Query::create()->select('m.id, m.nombre')->from('Municipio m')->where('m.entidad_id = '.sfContext::getInstance()->getUser()->getAttribute('agente_user_entidad_id') )->orderBy('m.nombre asc'),
         'expanded' => false));
       
       
       $this->widgetSchema['parroquia_id'] = new sfWidgetFormChoice(array(
                    'choices'  => array( '' => 'Seleccion Municipio'),
                    'expanded' => false,
                    'multiple' => false,
      ));
       

      $this->validatorSchema['pais_id'] = new sfValidatorPass();
      $this->validatorSchema['entidad_id'] = new sfValidatorPass();
      $this->validatorSchema['municipio_id'] = new sfValidatorPass();

      $this->widgetSchema->moveField('entidad_id', 'after', 'pais_id');
      $this->widgetSchema->moveField('municipio_id', 'after', 'entidad_id');
      $this->widgetSchema->moveField('parroquia_id', 'after', 'municipio_id');

      //Usuario Email
      $emails = $this->getObject()->getEmail();
      
      if(!count($emails)) {
          $emails = array();
      }     
      
      $emails_form = new sfForm();
      $count = 0;
      
      if($this->isNew()){
          $emailE = new EmailEtiqueta();
          $emailsE = $emailE->getEmailE(1);
          
           
          foreach ($emailsE as $emp ){
               $email = new Email();
               $email->setEmailEtiquetaId($emp->getId());
               $email->setUsuario($this->getObject());
               
               $ema_form = new EmailForm($email);
               $ema_form->widgetSchema['empresa_id'] = new sfWidgetFormInputHidden(array(),array('value'=> sfContext::getInstance()->getUser()->getAttribute('agente_user_empresa_id')));
               $ema_form->widgetSchema['email_etiqueta_id'] = new sfWidgetFormInputHidden();
               $ema_form->widgetSchema->setLabels(array('email_etiqueta_id'    => $emp->getNombre()));
               
               if($emp->getRequired()){
                    $ema_form->widgetSchema['required'] = new sfWidgetFormInputHidden();
               }else {
                    $ema_form->widgetSchema['id'] = new sfWidgetFormInputHidden(array(),array('value' => $email->getId()));  
               }
               $emails_form->embedForm($count, $ema_form);
               $count++;
               array_push ($emails, $email);
           }
           
           //Etiquetas no obligatorias
           
           $emailE = new EmailEtiqueta();
          $emailsE = $emailE->getEmailE(0);
          
           
          foreach ($emailsE as $emp ){
               $email = new Email();
               $email->setEmailEtiquetaId($emp->getId());
               $email->setUsuario($this->getObject());
               
               $ema_form = new EmailForm($email);
               $ema_form->widgetSchema['empresa_id'] = new sfWidgetFormInputHidden(array(),array('value'=> sfContext::getInstance()->getUser()->getAttribute('agente_user_empresa_id')));
               $ema_form->widgetSchema['email_etiqueta_id'] = new sfWidgetFormInputHidden();
               $ema_form->widgetSchema->setLabels(array('email_etiqueta_id'    => $emp->getNombre()));
               
               if($emp->getRequired()){
                    $ema_form->widgetSchema['required'] = new sfWidgetFormInputHidden();
               }else {
                    $ema_form->widgetSchema['id'] = new sfWidgetFormInputHidden(array(),array('value' => $email->getId()));
                    $ema_form->validatorSchema['email']->setOption('required', false);
               }
               $emails_form->embedForm($count, $ema_form);
               $count++;
               array_push ($emails, $email);
           }
           
      }else {

        foreach ($emails as $email){
            $ema_form = new EmailForm($email);
            $ema_form->widgetSchema['empresa_id'] = new sfWidgetFormInputHidden(array(),array('value'=> sfContext::getInstance()->getUser()->getAttribute('agente_user_empresa_id')));
            $ema_form->widgetSchema['email_etiqueta_id'] = new sfWidgetFormInputHidden();
            $ema_form->widgetSchema->setLabels(array('email_etiqueta_id'    => $email->getEmailEtiqueta()));
            if($email->getEmailEtiqueta()->getRequired()){
                $ema_form->widgetSchema['required'] = new sfWidgetFormInputHidden();
            }else {
                $ema_form->widgetSchema['id'] = new sfWidgetFormInputHidden(array(),array('value' => $email->getId()));  
                $ema_form->validatorSchema['email']->setOption('required', false);
            }
            $emails_form->embedForm($count, $ema_form);
            $count++;
             
      } }
      $this->embedForm('emails', $emails_form);
      // Fin Usuario Email

      
      //Usuario Telefono
      $telefonos = $this->getObject()->getTelefono();

      
      if(!count($telefonos)) {
          $telefonos = array();
      }
      
      $telefonos_form = new sfForm();
      $count = 0;
      
      if($this->isNew()){
          
          
          $telefonoE = new TelefonoEtiqueta();
          $telefonosE = $telefonoE->getTelefonoE(1);
          
          foreach ($telefonosE as $emp ){              
               $telefono = new Telefono();
               $telefono->setTelefonoEtiquetaId($emp->getId());
               $telefono->setUsuario($this->getObject());
               
               $tel_form = new TelefonoForm($telefono);
               $tel_form->widgetSchema['empresa_id'] = new sfWidgetFormInputHidden(array(),array('value'=> sfContext::getInstance()->getUser()->getAttribute('agente_user_empresa_id')));
               $tel_form->widgetSchema['telefono_etiqueta_id'] = new sfWidgetFormInputHidden();
               $tel_form->widgetSchema->setLabels(array('telefono_etiqueta_id'    => $emp->getNombre(),));
               if($emp->getRequired()){
                  $tel_form->widgetSchema['required'] = new sfWidgetFormInputHidden();
               }else {
                  $tel_form->widgetSchema['id'] = new sfWidgetFormInputHidden(array(),array('value' => $telefono->getId()));  
               }          
               
               $telefonos_form->embedForm($count, $tel_form);
               $count++;               
               array_push ($telefonos, $telefono);                
           }
           
         $telefonoE = new TelefonoEtiqueta();
          $telefonosE = $telefonoE->getTelefonoE(0);
          
          foreach ($telefonosE as $emp ){              
               $telefono = new Telefono();
               $telefono->setTelefonoEtiquetaId($emp->getId());
               $telefono->setUsuario($this->getObject());
               
               $tel_form = new TelefonoForm($telefono);
               $tel_form->widgetSchema['empresa_id'] = new sfWidgetFormInputHidden(array(),array('value'=> sfContext::getInstance()->getUser()->getAttribute('agente_user_empresa_id')));
               $tel_form->widgetSchema['telefono_etiqueta_id'] = new sfWidgetFormInputHidden();
               $tel_form->widgetSchema->setLabels(array('telefono_etiqueta_id'    => $emp->getNombre(),));
               if($emp->getRequired()){
                  $tel_form->widgetSchema['required'] = new sfWidgetFormInputHidden();
               }else {
                  $tel_form->widgetSchema['id'] = new sfWidgetFormInputHidden(array(),array('value' => $telefono->getId()));
                  $tel_form->validatorSchema['numero']->setOption('required', false);
               }          
               
               $telefonos_form->embedForm($count, $tel_form);
               $count++;               
               array_push ($telefonos, $telefono);                
           }
      }else {
          
        foreach ($telefonos as $telefono){
            
            $tel_form = new TelefonoForm($telefono);
            $tel_form->widgetSchema['empresa_id'] = new sfWidgetFormInputHidden(array(),array('value'=> sfContext::getInstance()->getUser()->getAttribute('agente_user_empresa_id')));
            $tel_form->widgetSchema['telefono_etiqueta_id'] = new sfWidgetFormInputHidden();
            $tel_form->widgetSchema->setLabels(array('telefono_etiqueta_id'    => $telefono->getTelefonoEtiqueta(),));
            
            
            if($telefono->getTelefonoEtiqueta()->getRequired()){
                
                $tel_form->widgetSchema['required'] = new sfWidgetFormInputHidden();
                
            }else {
                
                $tel_form->widgetSchema['id'] = new sfWidgetFormInputHidden(array(),array('value' => $telefono->getId()));  
                $tel_form->validatorSchema['numero']->setOption('required', false);
            }          
            
            $telefonos_form->embedForm($count, $tel_form);
            $count++;          
      }}

      $this->embedForm('telefonos', $telefonos_form);
      // Fin Usuario Telefono
      
      //Usuario Direccion
      $direccions = $this->getObject()->getDireccion();
      
      if(!count($direccions)) {
          $direccions = array();
      }
      
      $direccions_form = new sfForm();
      $count = 0;
      
      if($this->isNew()){
          $direccionE = new DireccionEtiqueta();
          $direccionsE = $direccionE->getDireccionE(1);
          
          foreach ($direccionsE as $emp ){
              
               $direccion = new Direccion();
               $direccion->setDireccionEtiquetaId($emp->getId());
               $direccion->setUsuario($this->getObject());
               
               $dir_form = new DireccionForm($direccion);
               $dir_form->widgetSchema['empresa_id'] = new sfWidgetFormInputHidden(array(),array('value'=> sfContext::getInstance()->getUser()->getAttribute('agente_user_empresa_id')));
               $dir_form->widgetSchema['direccion_etiqueta_id'] = new sfWidgetFormInputHidden();
               $dir_form->widgetSchema->setLabels(array('direccion_etiqueta_id'    => $emp->getNombre(),));
               if($emp->getRequired()){
                $dir_form->widgetSchema['required'] = new sfWidgetFormInputHidden();
               }else {
                $dir_form->widgetSchema['id'] = new sfWidgetFormInputHidden(array(),array('value' => $direccion->getId()));  
               }    
               
               $direccions_form->embedForm($count, $dir_form);
               $count++;               
               array_push ($direccions, $direccion);                
           }
           
           $direccionE = new DireccionEtiqueta();
          $direccionsE = $direccionE->getDireccionE(0);
          
          foreach ($direccionsE as $emp ){
              
               $direccion = new Direccion();
               $direccion->setDireccionEtiquetaId($emp->getId());
               $direccion->setUsuario($this->getObject());
               
               $dir_form = new DireccionForm($direccion);
               $dir_form->widgetSchema['empresa_id'] = new sfWidgetFormInputHidden(array(),array('value'=> sfContext::getInstance()->getUser()->getAttribute('agente_user_empresa_id')));
               $dir_form->widgetSchema['direccion_etiqueta_id'] = new sfWidgetFormInputHidden();
               $dir_form->widgetSchema->setLabels(array('direccion_etiqueta_id'    => $emp->getNombre(),));
               if($emp->getRequired()){
                $dir_form->widgetSchema['required'] = new sfWidgetFormInputHidden();
               }else {
                $dir_form->widgetSchema['id'] = new sfWidgetFormInputHidden(array(),array('value' => $direccion->getId()));
                $dir_form->validatorSchema['descripcion']->setOption('required', false);
               }    
               
               $direccions_form->embedForm($count, $dir_form);
               $count++;               
               array_push ($direccions, $direccion);                
           }
      }else {
      
        foreach ($direccions as $direccion){
            $dir_form = new DireccionForm($direccion);
            $dir_form->widgetSchema['empresa_id'] = new sfWidgetFormInputHidden(array(),array('value'=> sfContext::getInstance()->getUser()->getAttribute('agente_user_empresa_id')));
            $dir_form->widgetSchema['direccion_etiqueta_id'] = new sfWidgetFormInputHidden();
            $dir_form->widgetSchema->setLabels(array('direccion_etiqueta_id'    => $direccion->getDireccionEtiqueta(),));
            if($direccion->getDireccionEtiqueta()->getRequired()){
                $dir_form->widgetSchema['required'] = new sfWidgetFormInputHidden();
            }else {
                $dir_form->widgetSchema['id'] = new sfWidgetFormInputHidden(array(),array('value' => $direccion->getId()));  
                $dir_form->validatorSchema['descripcion']->setOption('required', false);
            }    
            $direccions_form->embedForm($count, $dir_form);
            $count++;
      }}

      $this->embedForm('direccions', $direccions_form);
      // Fin Usuario Direccion
      
       //Usuario Mascota
      $mascotas = $this->getObject()->getMascota();
      
      if(!count($mascotas)) {
          $mascotas = array();
      }
      $mascotas_form = new sfForm();
      $count = 0;
      foreach ($mascotas as $mascota){
          $mas_form = new MascotaForm2($mascota);
          $mascotas_form->embedForm($count, $mas_form);
          $count++;
      }

      $this->embedForm('mascotas', $mascotas_form);
      // Fin Mascota
      
      //Usuario Contactos Adicionales
      $parentescos = $this->getObject()->getParentesco();
      
      if(!count($parentescos)) {
          $parentescos = array();
      }
      $parentescos_form = new sfForm();
      $count = 0;
      foreach ($parentescos as $parentesco){
          $par_form = new ParentescoForm2($parentesco);
          $parentescos_form->embedForm($count, $par_form);
          $count++;
      }

      $this->embedForm('parentescos', $parentescos_form);
      // Contacto Adicionales
      
      
      //Usuario Periodiciadad
      $llamadaServicios = $this->getObject()->getLlamadaServicio();
      
      if(!count($llamadaServicios)) {
          $llamadaServicios = array();
      }
      $llamadaServicios_form = new sfForm();
      $count = 0;
      foreach ($llamadaServicios as $llamadaServicio){
          $lla_form = new LlamadaServicioForm2($llamadaServicio);
          $llamadaServicios_form->embedForm($count, $lla_form);
          $count++;
      }

      $this->embedForm('llamada_servicios', $llamadaServicios_form);
      // fin //Usuario Periodiciadad

      

      $this->widgetSchema->setLabels(array(
        'Comun'    => false,
        'User'    => false,
        'parroquia_id' => 'Colonia',
        'municipio_id'  => 'Municipio / Delegación'
        ));

  }
  
  public function addEmail($num) {
      
      $emailE = new EmailEtiqueta();
      $emailsE = $emailE->getEmailE(0);
      
      if($emailsE->count()!=0){
        
        $det = new Email();  
        $det->setEmailEtiqueta($emailsE->getFirst());
        $det->setUsuario($this->getObject());

        $det_form = new EmailForm($det);
        $this->embeddedForms['emails']->embedForm($num, $det_form);
        $this->embedForm('emails', $this->embeddedForms['emails']);
      }else {
          exit;
      }    
  }
  
  public function addExtra($num) {
      
      $extraE = new CamposEtiqueta();
      $extrasE = $extraE->getCamposE(0);
      
      if($extrasE->count()!=0){
          
        $det = new ExtrasCampo();
        $det->setCamposEtiqueta($extrasE->getFirst());
        $det->setUsuario($this->getObject());
        
        $det_form = new ExtrasCampoForm($det);
        $this->embeddedForms['detalles']->embedForm($num, $det_form);
        $this->embedForm('detalles', $this->embeddedForms['detalles']);
      }else{
          exit;
      }
  }
  
  public function addTelefono($num) {
      
      $telefonoE = new TelefonoEtiqueta();
      $telefonosE = $telefonoE->getTelefonoE(0);
      
      if($telefonosE->count()!=0){
        $det = new Telefono();
        $det->setTelefonoEtiqueta($telefonosE->getFirst());
        $det->setUsuario($this->getObject());

        $det_form = new TelefonoForm($det);
        $this->embeddedForms['telefonos']->embedForm($num, $det_form);      
        $this->embedForm('telefonos', $this->embeddedForms['telefonos']);   
      }else {
          exit;
      }   
  }
  
   public function addDireccion($num) {
      $direccionE = new DireccionEtiqueta();
      $direccionsE = $direccionE->getDireccionE(0); 
       
      if($direccionsE->count()!=0){ 
        $det = new Direccion();
        $det->setDireccionEtiqueta($direccionsE->getFirst());
        $det->setUsuario($this->getObject());
        $det_form = new DireccionForm($det);

        $this->embeddedForms['direccions']->embedForm($num, $det_form);

        $this->embedForm('direccions', $this->embeddedForms['direccions']);
      }else {
          exit;
      }
  }
  
  public function addMascota($num) {
      $det = new Mascota();
      $det->setUsuario($this->getObject());
      $det_form = new MascotaForm2($det);

      $this->embeddedForms['mascotas']->embedForm($num, $det_form);

      $this->embedForm('mascotas', $this->embeddedForms['mascotas']);

  }
  
  public function addParentesco($num) {
      $det = new Parentesco();
      $det->setUsuario($this->getObject());
      $det_form = new ParentescoForm2($det);

      $this->embeddedForms['parentescos']->embedForm($num, $det_form);

      $this->embedForm('parentescos', $this->embeddedForms['parentescos']);

  }
  
  public function addPeriodicidad($num) {
      
      $det = new LlamadaServicio();
      $det->setUsuario($this->getObject());
      $det_form = new LlamadaServicioForm2($det);

      $this->embeddedForms['llamada_servicios']->embedForm($num, $det_form);

      $this->embedForm('llamada_servicios', $this->embeddedForms['llamada_servicios']);

  }
  

  protected function removeField() {
       //Eliminar los campos del formularios  created_at y updated_at
      unset (
        $this['created_at'],
        $this['updated_at'],
        $this['nota_fija'],
        $this['user_id']
      );
  }
 
   public function  bind(array $taintedValues = null, array $taintedFiles = null) {

    //$this->getEmbeddedForm('Comun')->bind($taintedValues, $taintedFiles);
    $i=0;
    if($this->isNew()){
        $i++;
    }
      
    if(isset($taintedValues['emails'])){
       
        foreach ($taintedValues['emails'] as $key => $newPre)
        {
            if(!isset ($this['emails'][$key])){
                $this->addEmail($key);
            }
           if($i==0){
               $this->saveEmbeddedForms(); 
               $i++;
               
           }
        }        
    }
    
    if(isset($taintedValues['detalles'])){
        foreach ($taintedValues['detalles'] as $key => $newPre)
        {
            if(!isset ($this['detalles'][$key])){
                $this->addExtra($key);
            }
            if($i==0){
               $this->saveEmbeddedForms(); 
               $i++;
               
           }
        } 
    }
    
    if(isset($taintedValues['telefonos'])){
        foreach ($taintedValues['telefonos'] as $key => $newPre)
        {
            if(!isset ($this['telefonos'][$key])){
                $this->addTelefono($key);
            }
            if($i==0){
               $this->saveEmbeddedForms(); 
               $i++;
               
           }
        }
    }
    
    if(isset($taintedValues['direccions'])){
        foreach ($taintedValues['direccions'] as $key => $newPre)
        {
            if(!isset ($this['direccions'][$key])){
                $this->addDireccion($key);
            }
            if($i==0){
               $this->saveEmbeddedForms(); 
               $i++;
               
           }
        }
    }
    
    if(isset($taintedValues['mascotas'])){
        foreach ($taintedValues['mascotas'] as $key => $newPre)
        {
            if(!isset ($this['mascotas'][$key])){
                $this->addMascota($key);
            }
            if($i==0){
               $this->saveEmbeddedForms(); 
               $i++;
               
           }
        }
    }
    
    if(isset($taintedValues['parentescos'])){
        if($taintedValues['parentescos']){
            foreach ($taintedValues['parentescos'] as $key => $newPre)
            {
                if(!isset ($this['parentescos'][$key])){
                    $this->addParentesco($key);
                }
            }
            if($i==0){
               $this->saveEmbeddedForms(); 
               $i++;
               
           }
        }
    }
    
    if(isset($taintedValues['llamada_servicios'])){
        if($taintedValues['llamada_servicios']){
            foreach ($taintedValues['llamada_servicios'] as $key => $newPre)
            {
                if(!isset ($this['llamada_servicios'][$key])){
                    $this->addPeriodicidad($key);
                }
            }
            if($i==0){
               $this->saveEmbeddedForms(); 
               $i++;
               
           }
        }
    }
    
     parent::bind($taintedValues, $taintedFiles);
    }

  
  
}
